package com.cn.server.module.chat.service;

import com.cn.common.core.exception.ErrorCodeException;
import com.cn.common.core.model.ResultCode;
import com.cn.common.core.session.SessionManager;
import com.cn.common.core.spi.ChatSpiLoader;
import com.cn.common.module.ModuleId;
import com.cn.common.module.chat.ChatCmd;
import com.cn.common.module.chat.response.ChatResponse;
import com.cn.common.module.chat.response.ChatType;
import com.cn.server.Constant;
import com.cn.server.module.login.ImUser;
import com.cn.server.spi.IMsgCacheSpiDao;
import com.cn.server.spi.IUserSpiDao;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 聊天服务
 */
@Component
public class ChatServiceImpl implements ChatService {

    private IUserSpiDao iUserSpiDao;
    private IMsgCacheSpiDao iMsgCacheSpiDao;

    public ChatServiceImpl() {
        iUserSpiDao = ChatSpiLoader.load(IUserSpiDao.class);
        iMsgCacheSpiDao = ChatSpiLoader.load(IMsgCacheSpiDao.class);
    }

    @Override
    public void publicChat(String loginName, String content, int typeu) {

        ImUser imUser = iUserSpiDao.getImUserByLoginName(loginName);
        //获取所有在线用户
        Set<String> onlineUsers = SessionManager.getOnlinePlayers();
        //创建消息对象
        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatType(ChatType.PUBLIC_CHAT);
        chatResponse.setSendLoginName(imUser.getLoginName());
        chatResponse.setSendUserName(imUser.getUserName());
        chatResponse.setSendUserHeadUrl(imUser.getUserHeadUrl());
        chatResponse.setMessage(content);
        chatResponse.setTypeu(typeu);
        chatResponse.setSendTime(System.currentTimeMillis());
        //发送消息
        for (String targetLoginName : onlineUsers) {
            SessionManager.sendMessage(targetLoginName, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse);
        }

    }

    @Override
    public void privateChat(String loginName, String targetLoginName, String content, int typeu) {
        //不能和自己私聊
        if (loginName.equals(targetLoginName)) {
            throw new ErrorCodeException(ResultCode.CAN_CHAT_YOUSELF, Constant.CAN_CHAT_YOUSELF);
        }
        ImUser imUser = iUserSpiDao.getImUserByLoginName(loginName);
        //判断目标是否存在
        ImUser imTargetUser = iUserSpiDao.getImUserByLoginName(targetLoginName);
        if (imTargetUser == null) {
            throw new ErrorCodeException(ResultCode.USER_NO_EXIST, Constant.USER_NO_EXIST);
        }

        //创建消息对象
        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatType(ChatType.PRIVATE_CHAT);
        chatResponse.setSendLoginName(imUser.getLoginName());
        chatResponse.setSendUserName(imUser.getUserName());
        chatResponse.setTartgetLoginName(imTargetUser.getLoginName());
        chatResponse.setMessage(content);
        chatResponse.setSendUserHeadUrl(imUser.getUserHeadUrl());
        chatResponse.setSendTime(System.currentTimeMillis());
        chatResponse.setTypeu(typeu);
        //判断对方是否在线
        if (!SessionManager.isOnlineUser(targetLoginName)) {
            iMsgCacheSpiDao.saveOffLineUserMsg(chatResponse);
            throw new ErrorCodeException(ResultCode.USER_NO_ONLINE, Constant.USER_NO_ONLINE);
        } else {
            //给目标对象发送消息
            SessionManager.sendMessage(imTargetUser.getLoginName(), ModuleId.CHAT, ChatCmd.PRIVATE_CHAT, chatResponse);
        }
    }
}
